//
// Created by ssii on 2020/1/20.
//
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

int lengthOfLongestSubstring(char *s) {


    if (*s == '\0')return 0;
    // 划动窗口默认未1
    int k = 1;
    int cc[256] = {0};
    int m = 0;
    for (int j = 0; s[j] != '\0'; j++) {
        // printf("%c,%d\n",s[j],s[j]);
        // printf("%d,%d\n",cc[s[j]],s[j]);
        // 记录上一次的重复位置
        if (cc[s[j]] > m)
            //同时作为划动窗口的左边界
                m = cc[s[j]];
        //本次循环的最新位置 index+1
        cc[s[j]] = j+1;
        // 索引相- ，+1
        int r = (j - m) + 1;
        k = k > r ? k : r;
    }
    return k;
}

int lengthOfLongestSubstring(string s) {
    int freq[256] = {0};
    int l = 0, r = 0; //滑动窗口为s[l...r]
    int res = 0;
    while (l < s.size() && r < s.size()) {
        char &i = s[r];
        if (freq[i] == 0) {
            freq[s[r]]++;
            r++;
            res = max(res, r - l);
        } else {   //r已经到头 || freq[s[r+1]] == 1
            freq[s[l]]--;
            l++;
        }


    }
    return res;
}

int main() {
    lengthOfLongestSubstring("asdfdsaddsadsf");
    lengthOfLongestSubstring((char *) "a");
    return 0;
}